<template>
    <div class="container">
        <!-- 用户投诉关联 -->
        <!-- 管理平台--主体列表展示 -->
        <div class="ComUserListmanageMain">
            <el-form :inline="true" :model="formData" class="demo-form-inline" size="small">
                <el-form-item label="客服系统登录账号">
                    <el-input v-model="formData.userAccount" placeholder="请输入登录账号"></el-input>
                </el-form-item>
                <el-form-item label="客服系统用户名称">
                    <el-input v-model="formData.userName" placeholder="请输入用户名称"></el-input>
                </el-form-item>
                <el-form-item label="投诉平台工号">
                    <el-input v-model="formData.compId" placeholder="请输入投诉平台工号"></el-input>
                </el-form-item>
                <el-button
                    type="primary"
                    size="small"
                    icon="el-icon-search"
                    @click="hanlesearch"
                    :loading="searchLoading"
                >查询</el-button>
                <el-button
                    type="primary"
                    size="small"
                    icon="el-icon-refresh-right"
                    @click="handleReset"
                    :loading="resetLoading"
                >重置</el-button>
            </el-form>
            <div style="margin-bottom: 16px;">
                <el-button
                    type="primary"
                    size="small"
                    icon="el-icon-circle-plus-outline"
                    @click="hanlePush"
                >新增</el-button>
            </div>
            <el-table :data="tableData" width="100%" stripe border class="heard-bg">
                <el-table-column label="客服系统登录账号" prop="userAccount"></el-table-column>
                <el-table-column label="客服系统用户名称" prop="userName"></el-table-column>
                <el-table-column label="投诉平台工号" prop="compId"></el-table-column>

                <el-table-column label="操作">
                    <template slot-scope="scope">
                        <el-button
                            type="primary"
                            icon="el-icon-edit"
                            circle
                            @click="handleEdit(scope.row)"
                        />
                        <el-button
                            type="danger"
                            icon="el-icon-delete"
                            circle
                            @click="handleDelete(scope.row)"
                        />
                    </template>
                </el-table-column>
            </el-table>
            <!-- 分页 -->
            <div class="pagination">
                <Pagination
                    :hanleChangeCurrentPage="hanleChangeCurrentPage"
                    :handleChangeSize="handleChangeSize"
                    :currentPage="currentPage"
                    :pageSize="pageSize"
                    :total="total"
                />
            </div>
        </div>

        <el-dialog
            v-drag
            :close-on-click-modal="false"
            :visible.sync="Show"
            append-to-body
            center
            width="480px"
        >
            <div slot="title">{{EditTitle}}</div>
            <el-form
                :model="Edit"
                class="menu-setting"
                label-position="right"
                ref="Edit"
                label-width="160px"
                :rules="formRules"
                size="small"
            >
                <el-form-item label="客服系统登录账号" prop="userAccount">
                    <el-input
                        v-model="Edit.userAccount"
                        :disabled="!ifAddOrUpdate"
                        placeholder="请输入"
                        style="width: 200px;"
                    />
                </el-form-item>
                <el-form-item label="客服系统用户名称" prop="userName">
                    <el-input v-model="Edit.userName" placeholder="请输入" style="width: 200px;" />
                </el-form-item>
                <el-form-item label="投诉平台工号" prop="compId">
                    <el-input v-model="Edit.compId" placeholder="请输入" style="width: 200px;" />
                </el-form-item>
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button
                    size="small"
                    icon="el-icon-circle-close"
                    @click="Show = false"
                    class="cancelBtn"
                >取 消</el-button>
                <el-button
                    type="primary"
                    size="small"
                    @click="submitEdit"
                    class="submitBtn"
                    icon="el-icon-circle-check"
                >确定</el-button>
            </div>
        </el-dialog>
    </div>
</template>

<script>
import Pagination from '@/components/Pagination/Pagination.vue';

export default {
    name: 'ComUserList',
    components: {
        Pagination,
    },
    data() {
        return {
            formData: {
                // 客服系统登录账号
                userAccount: '',
                // 客服系统用户名称
                userName: '',
                // 投诉平台工号
                compId: '',
            },

            // 新增、编辑
            Edit: {
                // 客服系统登录账号
                userAccount: '',
                // 客服系统用户名称
                userName: '',
                // 投诉平台工号
                compId: '',
            },
            EditTitle: '',
            Show: false,
            // 搜索loading
            searchLoading: false,
            // 重置loading
            resetLoading: false,
            formRules: {
                userAccount: [
                    { required: true, message: '请输入登录账号', trigger: 'blur' },
                ],
                userName: [
                    { required: true, message: '请输入用户名称', trigger: 'blur' },
                ],

                compId: [
                    { required: true, message: '请输入投诉平台工号', trigger: 'blur' },
                ],
                // workplaceDesc: [
                //     { required: true, message: '请输入备注', trigger: 'blur' },
                //     { max: 51, message: '字符最大长度为51', trigger: 'blur' },
                // ]
            },
            // 当前页码
            currentPage: 1,
            // 页码大小
            pageSize: 10,
            // 总数
            total: 0,
            // 表格内容
            tableData: [],
            //下拉数据列表
            selectList: [],
            //判断新增还是编辑
            ifAddOrUpdate: Boolean
        };
    },
    mounted() {
        this.getInitData(this.formData);
    },
    methods: {
        // 获取职场表格数据
        getInitData(form) {
            let params = {
                pageNum: this.currentPage,
                pageSize: this.pageSize,
            }
            params = Object.assign({}, params, form)
            this.$post.ComUserList.searchByPage(params)
                .then((res) => {
                    console.log(res, 'ComUserList.searchByPage,res')
                    if (res.status == 200) {
                        this.tableData = res.data.content.compList;
                        this.total = res.data.content.compCount;
                    }
                });
        },

        // 点击搜索
        hanlesearch() {
            this.currentPage = 1;
            this.pageSize = 10;
            this.getInitData(this.formData);
        },
        // 点击重置
        handleReset() {
            this.formData = {
                // 客服系统登录账号
                userAccount: '',
                // 客服系统用户名称
                userName: '',
                // 投诉平台工号
                compId: '',
            };
            this.currentPage = 1;
            this.pageSize = 10;
            this.getInitData(this.formData);
        },
        // 点击新增
        hanlePush() {
            this.EditTitle = '添加关联';
            this.Edit = {};
            this.Show = true;
            this.ifAddOrUpdate = true;
            // 解决一进来就进行表单验证问题
            if (this.$refs.Edit) {
                this.$refs.Edit.resetFields();
            } else {
                this.$nextTick(() => {
                    // this.$refs.Edit.resetFields();
                });
            }
        },
        // 点击编辑
        handleEdit(row) {
            this.EditTitle = '修改关联';
            this.Edit = JSON.parse(JSON.stringify(row));
            this.Show = true;
            this.ifAddOrUpdate = false;
            // 解决一进来就进行表单验证问题
            if (this.$refs.Edit) {
                this.$refs.Edit.resetFields();
            } else {
                this.$nextTick(() => {
                    // this.$refs.Edit.resetFields();
                });
            }
        },
        // 单个删除租户职场
        handleDelete(row) {
            this.$confirm('确认删除该记录吗？', '提示', { customClass: 'warning', type: 'warning' })
                .then(() => {
                    this.$post.ComUserList.deleteByUserAccount({ userAccount: row.userAccount })
                        .then((res) => {
                            if (res.data.success) {
                                this.$message.success('删除成功！');
                                if (this.tableData.length === 1) {
                                    this.currentPage = this.currentPage - 1;
                                }
                                this.getInitData(this.formData);
                            } else {
                                this.$message.error('删除失败！');
                            }
                        });
                })
                .catch(() => {
                });
        },
        // 修改点击确定按钮
        submitEdit() {
            if (this.ifAddOrUpdate == false) {
                // 编辑
                this.$refs.Edit.validate((valid) => {
                    if (valid) {
                        this.$post.ComUserList.updateByUserAccount(this.Edit)
                            .then((res) => {
                                if (res.data.success) {
                                    this.$message.success('修改成功！');
                                    this.getInitData(this.formData);
                                    this.Show = false;
                                } else {
                                    this.$message.error(res.data.msg);
                                }
                            });
                    } else {
                        this.$message.warning('请完成表单填写！');
                    }
                });
            } else {
                // 添加
                this.$refs.Edit.validate((valid) => {
                    console.log(this.Edit)
                    if (valid) {
                        this.$post.ComUserList.add(this.Edit)
                            .then((res) => {
                                if (res.data.success) {
                                    this.$message.success('添加成功！');
                                    this.getInitData(this.formData);
                                    this.Show = false;
                                } else {
                                    this.$message.error(res.data.msg);
                                }
                            });
                    } else {
                        this.$message.warning('请完成表单填写！');
                    }
                });
            }
        },
        // 页码切换方法
        hanleChangeCurrentPage(val) {
            this.currentPage = val;
            this.getInitData();
        },
        // 切换分页大小方法
        handleChangeSize(val) {
            this.pageSize = val;
            this.getInitData();
        },
    },
};
</script>

<style  lang="scss">
.container {
    width: 100%;
    height: 100%;
    .ComUserListmanageMain {
        width: 100%;
        background: #fff;
        margin: 0 0 15px;
        // border-radius:5px;
        padding: 20px;
        box-sizing: border-box;
        text-align: left;
        .tableTitle {
            width: 100%;
            height: 48px;
            border: 1px solid #eee;
            border-bottom: none;
            line-height: 48px;
            margin-top: 20px;
            color: #666;
        }
        .pagination {
            margin-top: 20px;
            text-align: right;
        }
    }
}
</style>
